package com.orangelife.service.Importlogs;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.orangelife.utils.ProConfig;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

public class QiniuUtils extends ExcellBaseServiceImpl {
	private Logger logger = Logger.getLogger(QiniuUtils.class);

	public String createErrorLogFile(Map<Integer, String> logmap) {
		String ljdz = "";
		try {
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("导入错误信息.xls");
			sheet.setColumnWidth(1, getPOIWidthByExcelPiex(300));
			// 第一行
			Row row0 = sheet.createRow(0);
			HSSFFont font1 = workbook.createFont();
			HSSFCellStyle style1 = workbook.createCellStyle();
			setFontStyleAttribute(1, font1);
			SetCellStyleAttribute(1, style1, font1);
			Cell cell00 = row0.createCell(0);
			cell00.setCellStyle(style1);
			cell00.setCellValue("行号");

			Cell cell01 = row0.createCell(1);
			cell01.setCellStyle(style1);
			cell01.setCellValue("错误信息");

			Set<Integer> keyset = new TreeSet<Integer>(logmap.keySet());

			int i = 1;
			for (Integer key : keyset) {
				Row row = sheet.createRow(i);
				Cell cell0 = row.createCell(0);
				cell0.setCellStyle(style1);
				cell0.setCellValue(key);

				Cell cell1 = row.createCell(1);
				cell1.setCellStyle(style1);
				cell1.setCellValue(logmap.get(key));
				i++;

			}

			ByteArrayOutputStream os = new ByteArrayOutputStream();

			/*
			 * OutputStream fileOut = null; fileOut = new
			 * FileOutputStream("D:/log.xls"); workbook.write(fileOut);
			 * fileOut.close();
			 */
			workbook.write(os);
			byte[] bytes = os.toByteArray();

			// 设置好账号的ACCESS_KEY和SECRET_KEY
			String ACCESS_KEY = ProConfig.getProValues("ACCESS_KEY");
			String SECRET_KEY = ProConfig.getProValues("SECRET_KEY");
			// 要上传的空间
			String bucketname = ProConfig.getProValues("bucketname");
			// 上传到七牛后保存的文件名
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			String date = dateFormat.format(new Date());

			String key = date + ".xls";

			/*
			 * String key = DateUtil.DateToString(new Date(), "yyyyMMddHHmmss")
			 * + ".xls";
			 */
			// 上传文件的路径
			// 密钥配置
			Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
			// 创建上传对象
			UploadManager uploadManager = new UploadManager();

			// 调用put方法上传
			// Response res = uploadManager.put(FilePath, null,
			// auth.uploadToken(bucketname));
			@SuppressWarnings("unused")
			Response res = uploadManager.put(bytes, key,
					auth.uploadToken(bucketname));

			// http://7xir0t.com2.z0.glb.qiniucdn.com/9c7de7e1-3f47-4970-9c4e-470120ee2e7a.xls
			ljdz = ProConfig.getProValues("QINIU_URL") + key;
		} catch (Exception e) {

			logger.error(e.getMessage());
			logger.info(e.getMessage());

			e.printStackTrace();

		}

		return ljdz;

	}

}
